package net.kts.attraction.subway.smallpass.sqlite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.kts.attraction.subway.smallpass.model.StationName;



import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SubwayNameDaoSQLite extends SQLiteOpenHelper {
	
	private Map<Integer, String> stationNamewMap = new HashMap<Integer, String>();
	
	public SubwayNameDaoSQLite(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(android.database.sqlite.SQLiteDatabase arg0) {};
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
	public void stationNameSetting(String nationCode) {
		
		nationCode = this.checkNationCode(nationCode);
		
		//SELECT STATION_CD, KO FROM TBL_STATION_NAME
		String sql = "SELECT STATION_CD, " + nationCode +" FROM TBL_STATION_NAME ";
		//sql += " WHERE ";
		
		Cursor cursor = getReadableDatabase().rawQuery(sql, null);
		
		cursor.moveToFirst();
		
		int stationCode = 0;
		String stationName = null;
		
		while(!cursor.isAfterLast()) {
			
			stationCode = cursor.getInt(0);
			stationName = cursor.getString(1).trim();
			
			stationNamewMap.put(stationCode, stationName);
			
			cursor.moveToNext();
		}
		
		
	}
	
	public StationName selectStationName(int stationCode, String nationCode) {
		
		StationName stationName = null;
		
		nationCode = this.checkNationCode(nationCode);
		
		String sql = "SELECT " + nationCode +" FROM TBL_STATION_NAME ";
		sql += " WHERE ";
		sql += " STATION_CD = " + stationCode;
		
		Cursor cursor = getReadableDatabase().rawQuery(sql, null);
		
		cursor.moveToFirst();
		
		while(!cursor.isAfterLast()) {
			stationName = new StationName();
			
			stationName.setStationCode(stationCode);
			stationName.setStationName(cursor.getString(0).trim());
			stationName.setStationCodeList(this.selectLineNums(stationCode));
			
			if(stationName.getStationCodeList() == null) {
				stationName.setLineNum(this.selectLineNum(stationCode));
			}
			
			cursor.moveToNext();
		}
		
		return stationName;
	}
	
	public int selectLineNum(int stationCode ) {
		
		int lineNum = 0;
		String sql = "SELECT LINE_NUM FROM TBL_STATIONINFO ";
		sql += " WHERE STATION_CD = " + stationCode;
		
		Cursor cursor = getReadableDatabase().rawQuery(sql, null);
		
		cursor.moveToFirst();
		
		while(!cursor.isAfterLast()) {
			
			lineNum = cursor.getInt(0);
			
			cursor.moveToNext();
		}
		
		
		return lineNum;
	}
	
	private List<Integer> selectLineNums(int stationCode) {
		
		List<Integer> returnList = null;
		String[] temps = null;
		
		String sql = "SELECT LINENUM_STATION_CODE FROM TBL_TRANSFER_STATION ";
		sql += " WHERE STATION_CODE = " + stationCode;
		
		Cursor cursor = getReadableDatabase().rawQuery(sql, null);
		
		if(cursor == null) {
			return returnList;
		}
		
		cursor.moveToFirst();
		
		while(!cursor.isAfterLast()) {
			
			returnList = new ArrayList<Integer>();
			
			temps = cursor.getString(0).split(";");
			
			for(int indexI = 0 ; indexI < temps.length; indexI += 2) {
				returnList.add(Integer.valueOf(temps[indexI+1].trim()));
			}
			
			cursor.moveToNext();
		}
		
		return returnList;
	}
	
	
	
	
	
	
	// 모든 국가를 다처리 할수 없으니 ;;
	// 일단 이상한 국가(아랍어) 이딴곳이면 몽땅 영어로
	private String checkNationCode(String nationCode) {
		
		if(nationCode.equals("ko")) {
			return nationCode;
		}
		
		
		return "en";
	}
	
}
